# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import json
import urllib3

# 跳过 SSL 警告
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
}


def crawl_baidu_hotsearch():
    """
    抓取百度实时热搜榜（https://top.baidu.com/board?tab=realtime）
    无需 Cookie，无需登录，公开接口
    """
    url = "https://top.baidu.com/board?tab=realtime"
    try:
        resp = requests.get(
            url,
            headers=HEADERS,
            timeout=10,
            verify=False  # 跳过 SSL 校验
        )
        resp.encoding = 'utf-8'
        soup = BeautifulSoup(resp.text, 'html.parser')

        # 百度热搜榜数据实际由 JS 渲染，但关键数据藏在 <script> 中
        # 查找包含 "dataList" 的 script 标签
        scripts = soup.find_all('script')
        data = None
        for script in scripts:
            if script.string and 'dataList' in script.string:
                # 提取 JSON 数据部分
                start = script.string.find('dataList')
                if start != -1:
                    # 尝试提取 JSON 对象
                    json_str = script.string[start + 9:]  # 跳过 'dataList:'
                    # 截断到第一个 }; 或 }
                    brace_count = 0
                    in_string = False
                    escape = False
                    end = 0
                    for i, c in enumerate(json_str):
                        if escape:
                            escape = False
                            continue
                        if c == '\\':
                            escape = True
                            continue
                        if c == '"':
                            in_string = not in_string
                        if not in_string:
                            if c == '{':
                                brace_count += 1
                            elif c == '}':
                                brace_count -= 1
                                if brace_count == 0:
                                    end = i + 1
                                    break
                    if end:
                        json_part = json_str[:end]
                        try:
                            data = json.loads(json_part)
                            break
                        except:
                            continue
        if not data:
            # 备用方案：直接解析 HTML 结构（2025年百度热搜结构）
            items = soup.select('div.category-wrap_iQLoo.horizontal_1eKyQ')
            results = []
            for item in items:
                try:
                    rank = item.select_one('div.index_1Ew5p').get_text(strip=True)
                    title = item.select_one('div.c-single-text-ellipsis').get_text(strip=True)
                    hot = item.select_one('div.hot-index_1Bl1a').get_text(strip=True)
                    desc = ''
                    desc_elem = item.select_one('div.desc_3V32G')
                    if desc_elem:
                        desc = desc_elem.get_text(strip=True)
                    link = ''
                    link_tag = item.select_one('a')
                    if link_tag and link_tag.get('href'):
                        link = link_tag['href']
                    results.append({
                        'rank': rank,
                        'title': title,
                        'hot': hot,
                        'desc': desc,
                        'link': link
                    })
                except Exception as e:
                    continue
            return results

        # 如果解析出 JSON 数据
        results = []
        for item in data.get('data', []):
            results.append({
                'rank': item.get('rank', ''),
                'title': item.get('word', ''),
                'hot': str(item.get('hotScore', '')),
                'desc': item.get('desc', ''),
                'link': item.get('url', '')
            })
        return results

    except Exception as e:
        print(f"抓取失败: {e}")
        return []


if __name__ == '__main__':
    print("正在抓取百度实时热搜榜（https://top.baidu.com/board?tab=realtime）...")
    hot_list = crawl_baidu_hotsearch()

    if not hot_list:
        print("未能解析出热搜数据，请检查页面结构是否变化。")
    else:
        for item in hot_list[:50]:  # 打印前50条
            print(f"{item['rank']}. {item['title']}")
            print(f"   热度: {item['hot']}")
            if item['desc']:
                print(f"   摘要: {item['desc']}")
            if item['link']:
                print(f"   链接: {item['link']}")
            print("-" * 50)